package in.muton.edushare.components.messaging.recievepanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.components.messaging.util.EscapeHTML;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.Date;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.vo.MessageComments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Users;

public class CommentsPanel extends Panel {
	public CommentsPanel(String id, IModel<Messages> model) {
		super(id, model);
		this.message = model;
		
		Form<Void> f = null;
		add(f = new Form<Void>("comment") {
			@Override
			public void onSubmit() {
				setComment(EscapeHTML.processInputString(getComment()));
				Users currentUser = EduShareSession.get().getLoggedInUser();
				MessageComments messageComment = new MessageComments();
				messageComment.setComment(getComment());
				messageComment.setCreationTimestamp(new Date());
				messageComment.setMessage(message.getObject());
				message.getObject().setUpdateTimestamp(new Date());
				setComment("");
				if (!message.getObject().getAllMessageRecipients().contains(
						currentUser))
					message.getObject().addToAllMessageRecipients(currentUser);
				messageComment.setUser(currentUser);
				(((EduShareSession) getSession()).getSessionORMInterface())
						.commitAllChanges();
			}

			private static final long serialVersionUID = -7638506426940600441L;
		});

		f.add(new ProfileImage("image", new CayenneModel<Users>(EduShareSession
				.get().getLoggedInUser()),
				ApplicationConstants.FILE_SUFFIX_ICON));

		f.add(new TextArea<String>("message", new PropertyModel<String>(
				CommentsPanel.this, "comment")));
		f.add(new Button("submit"));
		add(f);
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	private String comment;
	private IModel<Messages> message = null;
	private static final long serialVersionUID = 1L;
}
